package com.sos.logging
{

	import com.sos.utils.ClassUtils;
	
	public class LogLevel
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		public static const FATAL:LogLevel 			= new LogLevel(0, "FATAL", 	1000);
		public static const ERROR:LogLevel 			= new LogLevel(1, "ERROR", 	8);
		public static const WARN:LogLevel 			= new LogLevel(2, "WARN", 	6);
		public static const INFO:LogLevel 			= new LogLevel(3, "INFO", 	4);
		public static const DEBUG:LogLevel 			= new LogLevel(4, "DEBUG", 	2);
		public static const ALL:LogLevel 			= new LogLevel(5, "ALL", 	0);
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<LogLevel>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<LogLevel>();
			_list.push(LogLevel.FATAL);
			_list.push(LogLevel.ERROR);
			_list.push(LogLevel.WARN);
			_list.push(LogLevel.INFO);
			_list.push(LogLevel.DEBUG);
			_list.push(LogLevel.ALL);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _value:int;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function LogLevel(id:uint, name:String, value:int)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("LogLevel enum already created!");
			// Init
			_id    = id;
			_name  = name;
			_value  = value;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------		
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get value():int
		{
			return _value;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function valid(level:LogLevel):Boolean
		{
			return _value >= level.value;
		}
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of 
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a LogLevel by given id 
		 * @param id Id of the card type to find
		 * @return PlayerRole constant for the given id, null if no LogLevel
		 * for the given id has been found
		 */		
		public static function getById(id:uint):LogLevel
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("LogLevel for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a LogLevel by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no LogLevel
		 * for the given name has been found
		 */		
		public static function getByName(name:String):LogLevel
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("LogLevel for name '" + name + "' unknown!");
		}
		
		/**
		 * Search a LogLevel by given sign 
		 * @param name sign of the player role to find
		 * @return PlayerRole constant for the given id, null if no LogLevel
		 * for the given sign has been found
		 */		
		public static function getByValue(value:int):LogLevel
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].value == value) return _list[i];
			}
			throw new Error("LogLevel for value '" + value + "' unknown!");
		}
		
	}
	
}